
var gulp = require('gulp');
var path = require('path');
var nui = require('gulp-nui');
var babel = require('babel-core');

var tomcaturl = path.join(__dirname,'./test');
var config = path.join(__dirname,'./test/config.js');
var files = ['jpg','png','gif','eot','svg','ttf','woff'];

// 使用gulp-nui配置options
var options = {
    base:tomcaturl,
    url:config,
    jsmin:{
        mangle:{
			except:['$']
		} 
    },
    cssmin:null,
    babel:[babel, {
		presets:[['es2015', {
			modules:false
		}], 'stage-2']
    }],
    filterPath:function(url, dirname){//过滤html文件中的部分路径
        return url;
    }
}
var pack = function(type){
    if(!type){
        delete options.mintype;
    }
    else{
        options.mintype = type;
    }
    gulp.src([tomcaturl+'/**/*.html'])
        .pipe(nui(options))
        .pipe(gulp.dest(tomcaturl));
}
gulp.task('nui',function(){
    pack();
})
//编译js
gulp.task('js',function(){
    return pack('js')
})
//编译css
gulp.task('css',function(){
    return pack('css')
})
// 监听css变化并且编译
// 监听js变化并且编译
// 修改config文件
// 对html文件修改版本号
gulp.task('rev', function(){
    gulp.src([tomcaturl +'/**/*-min.css'])
     .pipe(nui())
     .pipe(gulp.dest(tomcaturl))
})
gulp.task('watch', function(){
	gulp.watch([
		tomcaturl+'/**/*.js',
		'!'+tomcaturl+'/**/*-min.js', 
		'!'+config], ['js']);

	gulp.watch([
		tomcaturl+'/**/*.{css,less}',
		'!'+tomcaturl+'/**/*-min.css'], ['css']);

	gulp.watch([tomcaturl+'/**/*.{'+ files +'}', tomcaturl+'/**/*-min.css'], ['rev']);
});

gulp.task('default', ['nui',  'watch']); 



